package com.zzw.conf;

import com.alibaba.druid.support.http.WebStatFilter;
import net.sf.ehcache.constructs.web.filter.GzipFilter;
import net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Configuration
/**
* @Athor:xiezhongjiang
* @Description:定义一些Filter使用
* @Date:2017/10/26 11:25
* @Since:1.0.0
*/
public class FilterConfig {
    @Autowired
    private DruidDataSourceProperties dataSourceProperties;

    @Bean
    /**
    * @Athor:xiezhongjiang
    * @Description:配置ehcache的Gzip压缩
    * @Date:2017/10/26 11:24
    * @Param:[]
    * @return org.springframework.boot.web.servlet.FilterRegistrationBean
    */
    public FilterRegistrationBean gzipFilter(){
        FilterRegistrationBean gzipFilter = new FilterRegistrationBean(new GzipFilter());
        String[] arrs = {"*.js","*.css","*.json","*.html"};
        gzipFilter.setUrlPatterns(Arrays.asList(arrs));
        return gzipFilter;
    }

    //@Bean
    /**
    * @Athor:xiezhongjiang
    * @Description:配置页面缓存,页面缓存会自动开启GZIP压缩
    * @Date:2017/10/26 11:23
    * @Param:[]
    * @return org.springframework.boot.web.servlet.FilterRegistrationBean
    */
/*    public FilterRegistrationBean helloFilter(){
        FilterRegistrationBean helloFilter = new FilterRegistrationBean(new SimplePageCachingFilter());
        Map<String,String> maps = new HashMap<>();
        //设置参数
        maps.put("cacheName","test");
        helloFilter.setInitParameters(maps);
        //设置路径
        String[] arrs = {"/hello"};
        helloFilter.setUrlPatterns(Arrays.asList(arrs));
        return helloFilter;
    }*/
    @Bean
    /**
    * @Athor:xiezhongjiang
    * @Description:druid监控过滤
    * @Date:2017/10/26 11:24
    * @Param:[]
    * @return org.springframework.boot.web.servlet.FilterRegistrationBean
    */
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", dataSourceProperties.getExclusions());
        return filterRegistrationBean;
    }
    @Bean
    public FilterRegistrationBean charsetFilterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new CharacterEncodingFilter("UTF-8"));
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", dataSourceProperties.getExclusions());
        return filterRegistrationBean;
    }
}
